package com.liuceng.zen.service;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.ws.rs.core.MediaType;
import java.util.Date;

@Service
public class SmsSenderService {
  private final static Logger LOG = LoggerFactory.getLogger(SmsSenderService.class);

  @Value("${isSendMessage}")
  private Boolean isSendMessage;
  /**
   * 短信发送
   * @param mobile 手机号
   * @param content 发送内容
   * @return
   */
  public Date sender(String mobile, String content){
    Client client = Client.create();
    Date current = new Date();
    if(isSendMessage){
      client.addFilter(new HTTPBasicAuthFilter("api","8d01baaac48adee67d3a0a7e364acea8"));
      WebResource webResource = client.resource("http://sms-api.luosimao.com/v1/send.json");
      MultivaluedMapImpl formData = new MultivaluedMapImpl();
      formData.add("mobile", mobile);
      formData.add("message", content);
      ClientResponse response =  webResource.type( MediaType.APPLICATION_FORM_URLENCODED ).post(ClientResponse.class, formData);
      String textEntity = response.getEntity(String.class);
      recordLog(textEntity);//记录日志
    }
    return current;
  }

  public String testStatus(){
    Client client = Client.create();
    client.addFilter(new HTTPBasicAuthFilter("api","8d01baaac48adee67d3a0a7e364acea8"));
    WebResource webResource = client.resource( "http://sms-api.luosimao.com/v1/status.json" );
    MultivaluedMapImpl formData = new MultivaluedMapImpl();
    ClientResponse response =  webResource.get( ClientResponse.class );
    String textEntity = response.getEntity(String.class);
    int status = response.getStatus();
    return textEntity;
  }

  public void recordLog(String textEntity) {
    int error_code = 0;
    String error_msg = null;
    try {
      JSONObject jsonObj = new JSONObject( textEntity );
      error_code = jsonObj.getInt("error");
      error_msg = jsonObj.getString("msg");
      if(error_code==0){
        LOG.info(error_msg);
      }else{
        LOG.warn("错误码："+error_code+"，错误信息："+error_msg);
      }
    } catch (JSONException ex) {
      LOG.error(error_msg);
    }
    textEntity = testStatus();
    try {
      JSONObject jsonObj = new JSONObject( textEntity );
      error_code = jsonObj.getInt("error");
      if( error_code == 0 ){
        int deposit = jsonObj.getInt("deposit");
        LOG.info("Fetch deposit success :"+deposit);
      }else{
        error_msg = jsonObj.getString("msg");
        LOG.warn("Fetch deposit failed,code is "+error_code+",msg is "+error_msg);
      }
    } catch (JSONException ex) {
      LOG.error("Fetch deposit failed,code is "+error_code+",msg is "+error_msg);
    }
  }

}
